% low perfomance
clear all 
% clear bash
clc
% system menu display and command input
while true
    disp('====================student info management system====================');
    disp('please choose function');
    disp('1.add student(constructor must via matrix)');
    disp('2.delete student');
    disp('3.modify student');
    disp('4.add subject score');
    disp('5.modify subject score');
    disp('6.delete subject score');
    disp('7.generate a test student library');
    disp('8.averange score calculate');
    disp('9.sort');
    disp('10.exit');
    a = input('please input your command\n');
    switch a
        case 1 %add student(constructor must via matrix)
            if exist('student_arr','var')
                fprintf('student_arr already exists');
                n = input(', and how many students do you want to add?');
                for i = 1:n
                    fprintf('\n==========Note:this is the info of student %d==========\n', i);
                    name = input('please enter student name\n','s');
                    number = input('please enter student number\n','s');
                    major = input('please enter student major\n','s');
                    for j = 1:5
                        fprintf('score %d of 5\n',j);
                        class(j) = input('please enter student socre\n');
                    end
                    student_arr(size(student_arr) + 1,1) = student(name, number, major, class(1), class(2), class(3), class(4), class(5));
                end
            else
                n = input('how many students do we have now?\n');
                for i = 1:n
                    fprintf('\n==========Note:this is the info of student %d==========\n', i);
                    name = input('please enter student name\n','s');
                    number = input('please enter student number\n','s');
                    major = input('please enter student major\n','s');
                    for j = 1:5
                        fprintf('score %d of 5\n',j);
                        class(j) = input('please enter student socre\n');
                    end
                    student_arr(i,1) = student(name, number, major, class(1), class(2), class(3), class(4), class(5));
                end
                fprintf('successfully add %d students'' info\n', n);
                clear j;
            end
        case 2 %delete student
            if exist('student_arr', 'var')
                a = input('which student''s info do you want to delete, input name\n', 's');
                i = findStudent(student_arr, a);
                if i
                    fprintf('student %s deleted successfully\n', student_arr(i).name);
                    student_arr = deleteStu(student_arr, i);
                else
                    error('no such student');
                end
            else
                error('no students to delete');
            end
        case 3 %modify student
            if exist('student_arr','var')
                a = input('which student''s info do you want to modify, input name\n', 's');
                i = findStudent(student_arr, a);
                if i
                    nme = input('please input new student''s name\n','s');
                    num = input('please input new student''s number\n','s');
                    maj = input('please input new student''s major\n','s');
                    fprintf('%s => %s, %s => %s, %s => %s\n', student_arr(i, 1).name, nme, student_arr(i, 1).number, num, student_arr(i, 1).major, maj);
                    student_arr(i) = modifyStu(student_arr(i), nme, num, maj);
                else
                    error('no such student');
                end
            else
                error('no students fond');
            end
        case 4 %add subject score
            if exist('student_arr','var')
                a = input('which student''s info do you want to add, input name\n', 's');
                i = findStudent(student_arr, a);
                if i
                    n = input('how many courses do you want to add?\n');
                    for j = 1:n
                        fprintf('socre %d of %d', j, n);
                        temp = input('waiting for input\n');
                        score_arr(j,1) = temp;
                    end
                    student_arr(i,1) = addScore(student_arr(i,1), n, score_arr);
                    fprintf('%d courses added successfully\n', n);
                    clear score_arr;
                    clear temp;
                else
                    error('no such student');
                end
            else
                error('no students fond');
            end
        case 5 %modify subject score
            if exist('student_arr','var')
                a = input('which student''s info do you want to modify, input name\n', 's');
                i = findStudent(student_arr, a);
                if i
                    temp = input('which score do you want to modify?\n');
                    score_tmp = input('now you can modify it to\n');
                    student_arr(i,1).class(temp) = score_tmp;
                    disp('score modify complete');
                    clear temp;
                    clear score_tmp;
                else
                    error('no such student');
                end
            else
                error('no students fond');
            end
        case 6 %delete subject score
            if exist('student_arr', 'var')
                a = input('which student''s info do you want to delete, input name\n', 's');
                i = findStudent(student_arr, a);
                if i
                    temp = input('which score do you want to delete?\n');
                    student_arr(i,1) = delScore(student_arr(i,1), temp);
                    disp('score delete complete');
                    clear temp;
                else
                    error('no such student');
                end
            else
                error('no students fond');
            end
        case 7 %generate a test student library
            student_arr(1,1) = student('name1', 'number1', 'major1', unidrnd(100), unidrnd(100), unidrnd(100), unidrnd(100), unidrnd(100));
            student_arr(2,1) = student('name2', 'number2', 'major2', unidrnd(100), unidrnd(100), unidrnd(100), unidrnd(100), unidrnd(100));
            student_arr(3,1) = student('name3', 'number3', 'major3', unidrnd(100), unidrnd(100), unidrnd(100), unidrnd(100), unidrnd(100));
            student_arr(4,1) = student('name4', 'number4', 'major4', unidrnd(100), unidrnd(100), unidrnd(100), unidrnd(100), unidrnd(100));
            student_arr(5,1) = student('name5', 'number5', 'major5', unidrnd(100), unidrnd(100), unidrnd(100), unidrnd(100), unidrnd(100));
            student_arr(6,1) = student('name6', 'number6', 'major6', unidrnd(100), unidrnd(100), unidrnd(100), unidrnd(100), unidrnd(100));
            disp('generate a 6 students array successfully');
        case 8 %averange score calculate
            if exist('student_arr','var')
                temp = input('which course''s averange score do you want to calculate?\n');
                av = avCalculate(student_arr, temp);
                fprintf('course %d''s averange score is %f\n', temp, av);
                clear av;
                clear temp;
            else
                error('no students fond');
            end
        case 9 %sort
            if exist('student_arr','var')
                a = input('which course do you want to sort by? input course number\n');
                temp = myBubbleSort(student_arr, a);%problems.
                for i = 1:size(student_arr)
                    disp(student_arr(i).name);
                end
                fprintf('is previous, and\n');
                for i = 1:size(temp)
                    disp(temp(i).name);
                end
                fprintf('is sorted\n');
            else
                error('no student fond');
            end
        case 10 %exit
            disp('bye bye =w=');
            clear all;
            break
        otherwise
            error('input invalid');
    end
end
